﻿/* eslint-disable handle-callback-err */
// 数据更新操作符
// https://blog.csdn.net/wenwen360360/article/details/78339221
// http://chenzhou123520.iteye.com/blog/1637629
import addMethod from '../../utils'
const hostIp = require('../../getIp.js')

class DataBase {
  constructor () {
    // this.initMongoCluster()
    // this.initMongoDBYndj()
    this.initMethod()
  }

  // 初始化Cluster
  initMongoCluster () {
    this.MongoClient = require('mongodb').MongoClient
    this.username = 'kuzhi1900'
    this.password = 'huang15923956587'
    this.uri =
      'mongodb+srv://' +
      this.username +
      ':' +
      this.password +
      '@cluster0-gdmm8.mongodb.net/database'
  }

  // 初始化MongoDb in fantasy.ren
  initMongoDB () {
    this.MongoClient = require('mongodb').MongoClient
    this.username = 'root'
    this.password = 'huang'
    // this.password = 'root'

    if (hostIp.substr(0, 10) === '192.168.1.') {
      this.ip = '192.168.1.247'
    } else {
      this.ip = '106.14.170.46'
    }
    this.ip = '106.14.170.46'
    // this.ip = '192.168.1.247'
    // this.ip = '127.0.0.1'
    console.log('ip:' + this.ip)
    this.database = 'gdzc'
    this.collectionName = null
    // mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
    this.uri =
      'mongodb://' + this.username + ':' + this.password + '@' + this.ip
  }

  // 初始化MongoDb in fantasy.ren
  initMongoDBYd () {
    this.MongoClient = require('mongodb').MongoClient
    this.username = 'root'
    this.password = 'huang'
    this.ip = '192.168.1.247'
    this.database = 'yd'
    this.collectionName = null
    // mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
    this.uri =
      'mongodb://' + this.username + ':' + this.password + '@' + this.ip
  }

  initMongoDBYndj () {
    this.MongoClient = require('mongodb').MongoClient
    this.username = 'root'
    this.password = 'root'
    this.ip = '39.104.26.58'
    this.database = 'yndj'
    this.collectionName = null
    // mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
    this.uri =
      'mongodb://' + this.username + ':' + this.password + '@' + this.ip
  }

  // 实现重载
  initMethod () {
    addMethod(DataBase.prototype, 'findAll', this.findAll)
    addMethod(DataBase.prototype, 'findAll', this.findAll1)
    addMethod(DataBase.prototype, 'find', this.find)
    addMethod(DataBase.prototype, 'find', this.find1)
    addMethod(DataBase.prototype, 'find', this.find2)
    addMethod(DataBase.prototype, 'find', this.find3)
    addMethod(DataBase.prototype, 'find', this.find4)
    addMethod(DataBase.prototype, 'findOne', this.findOne)
    addMethod(DataBase.prototype, 'findOne', this.findOne0)
    addMethod(DataBase.prototype, 'findOne', this.findOne1)
    addMethod(DataBase.prototype, 'insert', this.insert)
    addMethod(DataBase.prototype, 'insert', this.insert1)
    addMethod(DataBase.prototype, 'updateOne', this.updateOne)
    addMethod(DataBase.prototype, 'updateOne', this.updateOne1)
    addMethod(DataBase.prototype, 'save', this.save)
    addMethod(DataBase.prototype, 'save', this.save0)
    addMethod(DataBase.prototype, 'save', this.save1)
    addMethod(DataBase.prototype, 'updateMany', this.updateMany)
    addMethod(DataBase.prototype, 'updateMany', this.updateMany1)
    addMethod(DataBase.prototype, 'deleteOne', this.deleteOne)
    addMethod(DataBase.prototype, 'deleteOne', this.deleteOne1)
    addMethod(DataBase.prototype, 'deleteMany', this.deleteMany)
    addMethod(DataBase.prototype, 'deleteMany', this.deleteMany1)
    addMethod(DataBase.prototype, 'deleteField', this.deleteField)
    addMethod(DataBase.prototype, 'deleteField', this.deleteField1)
    addMethod(DataBase.prototype, 'replaceOne', this.replaceOne)
  }

  setDatabase (database) {
    this.database = database
  }

  setCollection (collectionName) {
    this.collectionName = collectionName
  }

  run (collectionName, callback) {
    this.MongoClient.connect(
      this.uri,
      { useNewUrlParser: true },
      (err, client) => {
        if (err) {
          console.log(err)
          return
        }
        const collection = client.db(this.database).collection(collectionName)
        callback(collection)
        client.close()
      }
    )
  }

  findAll (collectionName, callback) {
    this.MongoClient.connect(
      this.uri,
      { useNewUrlParser: true },
      (err, client) => {
        if (err) {
          console.log(err)
          return
        }
        const collection = client.db(this.database).collection(collectionName)
        collection.find().toArray((err, result) => {
          callback(result)
          client.close()
        })
      }
    )
  }

  findAll1 (callback) {
    this.findAll(this.collectionName, callback)
  }

  find (collectionName, filter, callback) {
    console.log(collectionName, filter)
    this.MongoClient.connect(
      this.uri,
      { useNewUrlParser: true },
      (err, client) => {
        if (err) {
          console.log(err)
          return
        }
        const collection = client.db(this.database).collection(collectionName)
        collection.find(filter).toArray((err, result) => {
          if (err) {
            console.log(err)
            return
          }
          callback(result)
          client.close()
        })
      }
    )
  }

  find2 (collectionName, filter, sort, callback) {
    this.MongoClient.connect(
      this.uri,
      { useNewUrlParser: true },
      (err, client) => {
        if (err) {
          console.log(err)
          return
        }
        const collection = client.db(this.database).collection(collectionName)
        collection
          .find(filter)
          .sort(sort)
          .toArray((err, result) => {
            if (err) {
              console.log(err)
              return
            }
            callback(result)
            client.close()
          })
      }
    )
  }

  find3 (collectionName, filter, sort, skip, limit, callback) {
    this.MongoClient.connect(
      this.uri,
      { useNewUrlParser: true },
      (err, client) => {
        if (err) {
          console.log(err)
          return
        }
        const collection = client.db(this.database).collection(collectionName)
        collection
          .find(filter)
          .sort(sort)
          .skip(skip)
          .limit(limit)
          .toArray((err, result) => {
            if (err) {
              console.log(err)
              return
            }
            callback(result)
            client.close()
          })
      }
    )
  }

  find4 (collectionName, filter, sort, skip, limit, project, callback) {
    console.log('find4')
    this.MongoClient.connect(
      this.uri,
      { useNewUrlParser: true },
      (err, client) => {
        if (err) {
          console.log(err)
          return
        }
        const collection = client.db(this.database).collection(collectionName)
        collection
          .find(filter)
          .sort(sort)
          .skip(skip)
          .limit(limit)
          .project(project)
          .toArray((err, result) => {
            if (err) {
              console.log(err)
              return
            }
            callback(result)
            client.close()
          })
      }
    )
  }

  count (collectionName, filter, callback) {
    this.MongoClient.connect(
      this.uri,
      { useNewUrlParser: true },
      (err, client) => {
        if (err) {
          console.log(err)
          return
        }
        const collection = client.db(this.database).collection(collectionName)
        collection.count(filter, (err, result) => {
          if (err) {
            console.log(err)
            return
          }
          callback(result)
          client.close()
        })
      }
    )
  }

  find1 (filter, callback) {
    this.find(this.collectionName, filter, callback)
  }

  findOne (collectionName, filter, sort, callback) {
    this.MongoClient.connect(
      this.uri,
      { useNewUrlParser: true },
      (err, client) => {
        if (err) {
          console.log(err)
          return
        }
        const collection = client.db(this.database).collection(collectionName)
        // limit=1
        collection
          .find(filter)
          .sort(sort)
          .limit(1)
          .toArray((err, result) => {
            callback(result[0])
            client.close()
          })
      }
    )
  }

  findOne0 (collectionName, filter, callback) {
    this.findOne(collectionName, filter, { _id: -1 }, callback)
  }

  findOne1 (filter, callback) {
    this.findOne0(this.collectionName, filter, callback)
  }
  // 新增数据
  insert (collectionName, doc, callback) {
    this.MongoClient.connect(
      this.uri,
      { useNewUrlParser: true },
      (err, client) => {
        if (err) {
          console.log(err)
          return
        }
        const collection = client.db(this.database).collection(collectionName)
        collection.insertOne(doc, { safe: true }, (err, result) => {
          // console.log(result + '插入成功')
          callback(result)
          client.close()
        })
      }
    )
  }

  insert1 (doc, callback) {
    this.insert(this.collectionName, doc, callback)
  }

  // 更新数据
  updateOne (collectionName, filter, update, callback) {
    this.MongoClient.connect(
      this.uri,
      { useNewUrlParser: true },
      (err, client) => {
        if (err) {
          console.log(err)
          return
        }
        const collection = client.db(this.database).collection(collectionName)
        collection.updateOne(
          filter,
          { $set: update },
          { safe: true },
          (err, result) => {
            callback(result)
            client.close()
          }
        )
      }
    )
  }
  updateOne1 (filter, update, callback) {
    this.updateOne(this.collectionName, filter, update, callback)
  }

  save (update, callback) {
    this.updateOne(this.collectionName, { _id: update._id }, update, callback)
  }

  save0 (collection, update, callback) {
    this.updateOne(collection, { _id: update._id }, update, callback)
  }

  save1 (update) {
    this.updateOne(this.collectionName, { _id: update._id }, update, () => {})
  }

  updateMany (collectionName, filter, update, callback) {
    this.MongoClient.connect(
      this.uri,
      { useNewUrlParser: true },
      (err, client) => {
        if (err) {
          console.log(err)
          return
        }
        const collection = client.db(this.database).collection(collectionName)
        collection.updateMany(
          filter,
          { $set: update },
          { safe: true },
          (err, result) => {
            callback(result)
            client.close()
          }
        )
      }
    )
  }
  updateMany1 (collectionName, filter, update, callback) {
    this.updateMany(this.collectionName, filter, update, callback)
  }

  replaceOne (collectionName, filter, update, callback) {
    this.MongoClient.connect(
      this.uri,
      { useNewUrlParser: true },
      (err, client) => {
        if (err) {
          console.log(err)
          return
        }
        const collection = client.db(this.database).collection(collectionName)
        collection.replaceOne(filter, update, (err, result) => {
          callback(result)
          client.close()
        })
      }
    )
  }
  // save3 (collectionName, doc, callback) {
  //   this.MongoClient.connect(this.uri, { useNewUrlParser: true }, (err, client) => {
  //           if (err) {
  //      console.log(err)
  //     return
  //   }
  //     const collectionName = client.db(this.database).collectionName(collectionName)
  //     collectionName.save(doc, (err, result) => {
  //       callback(result)
  //     })
  //     client.close()
  //   })
  // }

  // 删除数据

  deleteOne (collectionName, filter, callback) {
    this.MongoClient.connect(
      this.uri,
      { useNewUrlParser: true },
      (err, client) => {
        if (err) {
          console.log(err)
          return
        }
        const collection = client.db(this.database).collection(collectionName)
        collection.deleteOne(filter, { safe: true }, (err, result) => {
          callback(result)
          client.close()
        })
      }
    )
  }
  deleteOne1 (filter, callback) {
    this.deleteOne(this.collectionName, filter, callback)
  }

  deleteMany (collectionName, filter, callback) {
    this.MongoClient.connect(
      this.uri,
      { useNewUrlParser: true },
      (err, client) => {
        if (err) {
          console.log(err)
          return
        }
        const collection = client.db(this.database).collection(collectionName)
        collection.deleteMany(filter, { safe: true }, (err, result) => {
          callback(result)
          client.close()
        })
      }
    )
  }
  deleteMany1 (filter, callback) {
    this.deleteMany(this.collectionName, filter, callback)
  }

  deleteField (collectionName, filter, update, callback) {
    this.MongoClient.connect(
      this.uri,
      { useNewUrlParser: true },
      (err, client) => {
        if (err) {
          console.log(err)
          return
        }
        const collection = client.db(this.database).collection(collectionName)
        collection.update(
          filter,
          { $unset: update },
          { multi: false },
          (err, result) => {
            callback(result)
            client.close()
          }
        )
      }
    )
  }
  deleteField1 (filter, update, callback) {
    this.deleteField(this.collectionName, filter, update, callback)
  }
}

module.exports = {
  DataBase
}
